﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using vukhoas.src.POJO;
using System.Data.SqlClient;
using vukhoas.src;

namespace vukhoas
{
    public partial class ListProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lst = new List<Product>();
                if(Request.QueryString["cate"]!=null)
                {
                    lst = lstProduct("", Request.QueryString["cate"],"");
                }
                else if (Request.QueryString["manu"] != null)
                {
                    lst = lstProduct("", "", Request.QueryString["manu"]);
                }
                else if (Request.QueryString["name"] != null)
                {
                    lst = lstProduct(Request.QueryString["name"], "","");
                }
                else
                {
                    lst = lstProduct("", "", "");
                }

                
            }
        }

        protected List<Category> lstCategory()
        {
            List<Category> lst = new List<Category>();
            SqlConnection conn = MyConnect.getConnection();
            conn.Open();
            try
            {
                String sql = "SELECT * FROM category";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Category pro = new Category();
                    pro.Id = dr.GetString(0);
                    pro.Name = dr.GetString(1);
                    pro.Description = dr.GetString(2);
                    lst.Add(pro);
                }
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return lst;
        }

        protected List<Manufacture> lstManufacture()
        {
            List<Manufacture> lst = new List<Manufacture>();
            SqlConnection conn = MyConnect.getConnection();
            conn.Open();
            try
            {
                String sql = "SELECT * FROM manufacture";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Manufacture pro = new Manufacture();
                    pro.Id = dr.GetString(0);
                    pro.Name = dr.GetString(1);
                    pro.Description = dr.GetString(2);
                    lst.Add(pro);
                }
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return lst;
        }

        protected List<Product> lst;
        private List<Product> lstProduct(String name, String cate, String manu)
        {
            
            List<Product> lst = new List<Product>();
            SqlConnection conn = MyConnect.getConnection();
            conn.Open();
            try
            {
                String sql = "SELECT * FROM product WHERE name LIKE '%" + name + "%' AND category_id LIKE '%" + cate + "%' AND manufacture_id LIKE '%"+manu+"%'";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Product pro = new Product();
                    pro.Id = dr.GetInt32(0);
                    pro.Name = dr.GetString(1);
                    pro.Price = dr.GetString(2);
                    pro.ManufactureID = dr.GetString(3);
                    pro.CategoryID = dr.GetString(4);
                    pro.Detail = dr.GetString(5);
                    pro.Introduce = dr.GetString(6);
                    pro.Information = dr.GetString(7);
                    pro.Image = dr.GetString(8);
                    pro.CreatedDate = dr.GetDateTime(9);
                    lst.Add(pro);
                }
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
            }
            finally
            {
                conn.Close();
            }
            return lst;
        }        
    }
}